/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import db.DBAccess;
import entity.Manager;
import entity.Student;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author DucND
 */
public class ManagerDAO {
    public static void delete(String username){
         try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con =
                    DriverManager.getConnection("jdbc:odbc:SMS");
            PreparedStatement prst =
                    con.prepareStatement("Delete from Manager where UserName = '"+username+"'");
            prst.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(ManagerDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ManagerDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public static void edit(Manager sta) throws ClassNotFoundException, SQLException{
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con = 
                    DriverManager.getConnection("jdbc:odbc:SMS");
            PreparedStatement prst =
                    con.prepareStatement("Update Manager set Password = '"+sta.getPassword()+"', Role = "+sta.isRole()+"' where Username = '"+sta.getUsername()+"'");
            prst.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(ManagerDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ManagerDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
    
    public static Manager getStaff(String Username){
        Manager sta= new Manager();
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con = DriverManager.getConnection("jdbc:odbc:SMS");
            PreparedStatement prst =
                    con.prepareStatement("select * from Manager where Username = '"+Username+"'");
            ResultSet rs = prst.executeQuery();
            rs.next();
            sta.setUsername(rs.getString("Username"));
            sta.setPassword(rs.getString("Password"));
            sta.setRole(rs.getBoolean("Role"));
        } catch (SQLException ex) {
            Logger.getLogger(ManagerDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ManagerDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return sta;
    }
    
    public static String login(String username, String password) {
        Connection conn = DBAccess.getConnection();
        try {
            PreparedStatement prst = conn.prepareStatement("Select Username from Manager where Username = ? and Password = ?");
            prst.setString(1, username);
            prst.setString(2, password);
            ResultSet rs = prst.executeQuery();
            if (rs != null && rs.next()) {
                return rs.getString("Username");
            }
        } catch (SQLException ex) {
            Logger.getLogger(ManagerDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(ManagerDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return null;
    }
}
